package com.zscat.mallplus.center.tencent;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.zscat.mallplus.utils.CommonResult;

import cn.hutool.core.lang.UUID;

/**
 * 文件上传控制器
 */
@RestController
@RequestMapping(value = "/cos")
public class CosController {

	private String accessKey = "AKIDJQL4ZIJJLA1dybvesxq30g8l8E5hENSX";
	private String secretKey = "EF3h6QYSrOra5TzNYWfzqS1UkpBSUHtI";
	private String bucket = "ap-nanjing";
	private String bucketName = "public-1301924604";
	private String path = "https://public-1301924604.cos.ap-nanjing.myqcloud.com/";
	private String qianzui = "image";
	/**
	 * 上传到腾讯云服务器（https://cloud.tencent.com/document/product/436/10199）
	 * @return
	 */
	@RequestMapping(value = "/upload",method = RequestMethod.POST)
	@ResponseBody
	public Object Upload(@RequestParam(value = "file") MultipartFile file, HttpSession session){
		if(file == null){
			return new CommonResult().failed("上传文件失败");
		}
		String oldFileName = file.getOriginalFilename();
		String eName = oldFileName.substring(oldFileName.lastIndexOf("."));
		String newFileName = UUID.randomUUID()+eName;
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month=cal.get(Calendar.MONTH);
		int day=cal.get(Calendar.DATE);
		// 1 初始化用户身份信息(secretId, secretKey)
		COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
		// 2 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
		ClientConfig clientConfig = new ClientConfig(new Region(bucket));
		// 3 生成cos客户端
		COSClient cosclient = new COSClient(cred, clientConfig);
		// bucket的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
		String bucketName = this.bucketName;

		// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
		// 大文件上传请参照 API 文档高级 API 上传
		File localFile = null;
		try {
			localFile = File.createTempFile("temp",eName);
			file.transferTo(localFile);
			// 指定要上传到 COS 上的路径
			String key = this.qianzui+"/"+year+"/"+month+"/"+day+"/"+newFileName;
			PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
			PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
			return new CommonResult().success(this.path + putObjectRequest.getKey());
		} catch (IOException e) {
			return new CommonResult().failed(e.getMessage());
		}finally {
			// 关闭客户端(关闭后台线程)
			cosclient.shutdown();
		}
	}
    
}
